import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:qunaimedical/values/urls.dart';

class CustomAvatar extends StatelessWidget {
  final double? size;
  final EdgeInsetsGeometry? margin;
  final String? avatarUrl;

  const CustomAvatar({
    Key? key,
    this.size,
    this.margin,
    this.avatarUrl,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    String? path;
    if (avatarUrl != null && avatarUrl != '') {
      if (avatarUrl!.startsWith('http')) {
        path = avatarUrl;
      } else {
        path = '${Urls.BASE_URL}/bokeupload$avatarUrl';
      }
    }
    final avatarSize = size ?? 50;
    final placeholder = Container(
      color: Colors.grey[200],
      width: avatarSize,
      height: avatarSize,
    );
    final defaultAvatar = Container(
      child: Image.asset('images/contact_default_avatar.png'),
      width: avatarSize,
      height: avatarSize,
    );
    return Container(
      margin: margin,
      child: ClipOval(
        child: avatarUrl == null || avatarUrl == ''
            ? defaultAvatar
            : CachedNetworkImage(
                imageUrl: path!,
                height: avatarSize,
                width: avatarSize,
                placeholder: (c, url) => placeholder,
                fit: BoxFit.cover,
              ),
      ),
    );
  }
}
